package com.china.controller;


import com.china.service.IChatHistoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;


@RequiredArgsConstructor
@RestController
@RequestMapping("/ai")
public class CustomerServiceController {

    @Autowired
    private  ChatClient ruleChatClient;

    @Autowired
    private IChatHistoryService chatHistoryService;
    /**
     * 流式调用
     * Flux是异步非阻塞
     * @param prompt
     * @return
     */
    @GetMapping(value = "/service", produces = "text/html;charset=UTF-8")
    public  Flux<String> stream(String prompt,String chatId) {
        chatHistoryService.saveChatHistory("service", chatId);
        return ruleChatClient.prompt(prompt)
                .user(prompt)
                .advisors(advisorSpec -> advisorSpec.param(ChatMemory.CONVERSATION_ID,chatId))
                .stream()
                .content();
    }
}